package system.back;

import java.util.*;

import system.struct.ParserResult;
import system.struct.SearchEntry;
import system.util.Util;

/**
 * Ranker of system
 * <p>
 * Used to rank the <tt>SearchEntry</tt> list of final result
 * 
 * @author Jack
 * 
 */
public class Ranker {

	/**
	 * Returns a ranked List of <tt>SearchEntry</tt> according to given
	 * <tt>ParserResult</tt> information
	 * <p>
	 * this implementation has to calculate each <tt>SearchEntry</tt>'s distance
	 * towards given <tt>ParserResult</tt>
	 * 
	 * @param pr
	 *            given <tt>ParserResult</tt>
	 * @param list
	 *            List of <tt>SearchEntry</tt> to be sorted
	 * @return a sorted list of <tt>SearchEntry</tt>
	 */
	public SearchEntry[] rank(ParserResult pr, ArrayList<SearchEntry> list) {
		SearchEntry[] ans = new SearchEntry[0];
		ans = list.toArray(ans);
		for (int i = 0; i < ans.length; i++) {
			/* calculate the distance of each SearchEntry away from ParserResult's keyword */
			ans[i].setDistance(Util.distance(ans[i].keywords(), pr.getKeyword()));
		}
		Arrays.sort(ans);
		return ans;
	}
}
